const dosql = require("../utils/doSql.js");
const update = dosql.updata;
const insert = dosql.insert;
const query = dosql.query;
const v = require("../utils/verify.js")
const v2 = require("../utils/verify2.js")


// 查询所有菜单  分级展示
exports.queryMenu = async (req, res) => {

    let level = 0;
    function buildTree(data, parentId, level = 0) {
        const nodes = data.filter(node => node.parent_id === parentId);
        return nodes.map(node => {
            const children = buildTree(data, node.menu_id, level + 1);
            if (children.length) {
                return { ...node, children, level: level + 1 };
            } else {
                return { ...node, level: level + 1 };
            }
        });
    }
    const sql1 = `SELECT * FROM menu where exist = 1 ORDER BY menu_id ASC`
    const rows = await query(sql1);
    res.sd(buildTree(rows, 0));
}


// 查询当前角色的权限
exports.queryPow = async (req, res) => {
    if (v(req, ["role_id"], res) !== 0) {
        return;
    }
    let role_id = req.query.role_id;
    const sql = `select * from role_menu where role_id = ?;`
    let result = await query(sql, [role_id]);
    res.sd(result)
}

// 修改当前角色权限
exports.upmenurole = async (req, res) => {
    if (v2(req, ["role_id"], res) !== 0) {
        return;
    }
    try {
        const addMenu = async (role_id, menu_ids) => {
            const sql = `insert into role_menu (menu_id,role_id) values (?,?)`;
            for (let i = 0; i < menu_ids.length; i++) {
                await insert(sql, [menu_ids[i], role_id]);
            }
        }
        const delMenu = async (role_id, menu_ids) => {
            const sql = `delete from role_menu where role_id = ? and menu_id = ?`;
            for (let i = 0; i < menu_ids.length; i++) {
                await update(sql, [role_id, menu_ids[i]]);
            }
        }
        let role_id = req.body.role_id;
        let menu_ids = req.body.menu_ids;
        // 首先获取这个角色的所有权限
        const sql0 = `select menu_id from role_menu where role_id = ?;`
        let result0 = await query(sql0, [role_id])
        // 如果菜单权限表中没有记录 则全部插入
        if (result0.length === 0) {
            console.log("全部插入");
            await addMenu(role_id, menu_ids)
        }
        // 如果菜单权限中有记录, 则进行对比 删除没有的, 添加新增的
        // 首先获取已有的权限列表
        let menuArr = (result0.map((item) => item.menu_id));
        // 然后分出 与B数组相比 A中新增的 和A中没有了的  A是发来的权限列表
        const missingElements = menuArr.filter((element) => !new Set(menu_ids).has(element));
        const newElements = menu_ids.filter((element) => !new Set(menuArr).has(element));
        // 添加新增的
        await addMenu(role_id, newElements);
        // 删除减少的
        await delMenu(role_id, missingElements);
        res.sd("ok")
    } catch (e) {
        res.sdErr(e);
    }
}

// 添加菜单
exports.addMenu = async (req, res) => {
    if (v2(req, ["parent_id", "menu_name"], res) !== 0) {
        return;
    }
    let parent_id = req.body.parent_id;
    let menu_name = req.body.menu_name;
    const sql = `insert into menu (parent_id, menu_name) values (?,?);`
    try {
        await update(sql, [parent_id, menu_name]);
        res.sd("ok")
    } catch (e) {
        res.sdErr(e);
    }

}
// 更新菜单
exports.upMenu = async (req, res) => {
    if (v2(req, ["menu_name", "menu_id"], res) !== 0) {
        return;
    }
    let menu_name = req.body.menu_name;
    let menu_id = req.body.menu_id;
    const sql = `update menu set menu_name=? where menu_id=?;`
    try {
        await update(sql, [menu_name, menu_id]);
        res.sd("ok")
    } catch (e) {
        res.sdErr(e);
    }

}
// 删除菜单
exports.delMenu = async (req, res) => {
    if (v2(req, ["menu_id"], res) !== 0) {
        return;
    }
    let menu_id = req.body.menu_id;
    const sql = `update menu set exist=0 where menu_id=?;`
    try {
        await update(sql, [menu_id]);
        res.sd("ok")
    } catch (e) {
        res.sdErr(e);
    }

}